/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.auction.service.impl;

import co.yixiang.modules.auction.domain.AuctionOrder;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import co.yixiang.dozer.service.IGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.auction.service.AuctionOrderService;
import co.yixiang.modules.auction.service.dto.AuctionOrderDto;
import co.yixiang.modules.auction.service.dto.AuctionOrderQueryCriteria;
import co.yixiang.modules.auction.service.mapper.AuctionOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author yhh
* @date 2021-02-18
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "AuctionOrder")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AuctionOrderServiceImpl extends BaseServiceImpl<AuctionOrderMapper, AuctionOrder> implements AuctionOrderService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(AuctionOrderQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<AuctionOrderDto> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), AuctionOrderDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<AuctionOrderDto> queryAll(AuctionOrderQueryCriteria criteria){
        return baseMapper.getAuctionList(criteria);
//        return baseMapper.selectList(QueryHelpPlus.getPredicate(AuctionOrder.class, criteria));
    }


    @Override
    public void download(List<AuctionOrderDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AuctionOrderDto AuctionOrder : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("拍卖id", AuctionOrder.getAuctionRecordId());
            map.put("状态0待付款1已上传凭证待确定2待操作3历史订单", AuctionOrder.getStatus());
            map.put("创建时间", AuctionOrder.getCreateTime());
            map.put("订单号", AuctionOrder.getOrderId());
            map.put("用户id", AuctionOrder.getUserId());
            map.put("收货人手机号", AuctionOrder.getUserPhone());
            map.put("收货人姓名", AuctionOrder.getUserName());
            map.put("收货人地址", AuctionOrder.getUserAddress());
            map.put("价格", AuctionOrder.getPrice());
            map.put("支付时间", AuctionOrder.getPayTime());
            map.put("支付方式1支付宝2微信3余额4银联", AuctionOrder.getPayType());
            map.put(" updateTime",  AuctionOrder.getUpdateTime());
            map.put("0未作处理1加入抢拍2收货", AuctionOrder.getReceiveType());
            map.put("抢拍手续费", AuctionOrder.getServiceMoney());
            map.put("是否支付手续费0否1是", AuctionOrder.getIsPayService());
            map.put("是否删除0否1是", AuctionOrder.getIsDel());
            map.put("是否达到最大转拍值", AuctionOrder.getIsMax());
            map.put("快递单号", AuctionOrder.getExpressNumber());
            map.put("快递名", AuctionOrder.getExpress());
            map.put("卖出价", AuctionOrder.getSellPrice());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
